package com.atguigu.srb.sms;

import com.atguigu.srb.commom.exception.Assert;
import com.atguigu.srb.commom.result.R;
import com.atguigu.srb.commom.result.ResponseEnum;
import com.atguigu.srb.commom.untis.RandomUtils;
import com.atguigu.srb.commom.untis.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.untis.SmsProperties;
import com.sun.org.apache.bcel.internal.classfile.Code;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.scene.control.Alert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")

@Slf4j
public class ApiSmsController {

    @Resource
    SmsService smsService;
    @Resource
    CoreUserInfoClient coreUserInfoClient;
    @Resource
    RedisTemplate redisTemplate;
    @ApiOperation("获取验证码")
    @GetMapping("/send/{mobile}")
    public R send(
                   @ApiParam(value = "手机号码",required = true)
                   @PathVariable
                    String mobile){
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        String code = RandomUtils.getFourBitRandom();
        System.out.println(code);
        boolean checkMobile = coreUserInfoClient.checkMobile(mobile);
        Assert.isTrue(checkMobile ==false,ResponseEnum.MOBILE_EXIST_ERROR);
//        String code = UUID.randomUUID().toString();

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
//        smsService.send(mobile, SmsProperties.TEMPLATE_CODE,map);

        redisTemplate.opsForValue().set("srb:sms:code"+mobile,code,5, TimeUnit.SECONDS);
        return  R.ok().message("短信发送成功");
    }

}
